package com.example.ccj.b.c.dialog;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.ccj.R;
import com.example.ccj.b.c.draw.SelfCircleProgressView;
import com.example.ccj.u.ImageUtils;


/**
 * loading
 * 包含进度条、错误文本提示、文本提示、转圈的加载
 */
public class LoadingDialog extends DialogBase{
    private LinearLayout mLlUploadFile;
    private LinearLayout mLlLoadingView;
    private ImageView mIvLoading;
    private SelfCircleProgressView uploadProgress;
    private TextView progressText;
    private int maxSum;

    public LoadingDialog(@NonNull Context context) {
        super(context);
        speed = 0;
    }


    @Override
    public void onInit() {
        isCanClose(false);
        setLayout(R.layout.dialog_load_net_work_data);
        noInstallWidth();//不沾满宽度
        setAlpha(0.8f);//透明度
        setCancelable(false);//点击不允许关闭
        isShowBackground(false);//不显示背景
        mLlUploadFile = findViewById(R.id.dialog_progress_upload_layout_1);
        mLlLoadingView = findViewById(R.id.loading_layout_3);
        mIvLoading = findViewById(R.id.loading_img);
        mIvLoading.setImageResource(R.mipmap.loading_dialog);
        uploadProgress = findViewById(R.id.upload_layout);
        progressText = findViewById(R.id.dialog_progress_upload_text);
        ImageUtils.loadImage(getContext(),R.mipmap.loading_5,mIvLoading);
    }

    /**
     * 显示加载等待框
     */
    public void showLoading() {
        if (!isShowing()) this.show();
        mLlUploadFile.setVisibility(View.GONE);
        mLlLoadingView.setVisibility(View.VISIBLE);
//        ImageUtils.loadImage(getContext(),R.mipmap.loading_5,mIvLoading);

    }

    /**
     * 显示圆形上传进度条
     * @param max 进度条的最大值
     */
    public void showUpload(int max){
        speed = 0;
        if (!isShowing()) this.show();
        setCancelable(false);//点击不允许关闭
        isShowBackground(false);//不显示背景
        mLlLoadingView.setVisibility(View.GONE);
        mLlUploadFile.setVisibility(View.VISIBLE);
        this.maxSum = max;
        uploadProgress.setMax(max);
    }

    private int speed = 0;


    /**
     * 填写进度
     * @param speed 进度
     */
    public void setSpeed(int speed){
        if (speed < this.speed) return;
        this.speed = speed;
        uploadProgress.post(new Runnable() {
            @Override
            public void run() {
                uploadProgress.setCurrent(speed);
            }
        });
        if (speed >= maxSum) dismiss();
    }

    /**
     * 填写进度文本
     * @param text 进度提示
     */
    public void setUploadText(String text){
        progressText.post(new Runnable() {
            @Override
            public void run() {
                progressText.setText(text);
            }
        });
    }

}
